dofile(os.getenv('LUAR_HOME') .. '/processor/ApplicationProcessor.lua')
dofile(os.getenv('LUAR_HOME') .. '/util/File.lua')
dofile(os.getenv('LUAR_HOME') .. '/util/Xml.lua')

function execute()
	
	local file = File{}

	if not file:exists('.manifest') then
		print 'Error generating application. Manifest does not exist.'
		return false
	end
	
	manifest = file:readFile('.manifest')
	
	if string.match( manifest, '<type>(.*)</type>' ) ~= 'luar-application' then
		print 'Error generating application. The current directory is not a Luar project.'
		return false
	end
	
	--luarcode = file:readFile( 'luar-app/main.luar' )
	luarcode = file:readFile( 'main.luar' )
	
	local xml = Xml{}
	local app_proc = ApplicationProcessor{}
	local nclcode
	ncl_trees = {}
	
	if string.find( luarcode, 'includeTemplate%(.*%)' ) then
		nclcode = app_proc:processTemplate( luarcode )
	else
		nclcode = app_proc:processApplication( luarcode )	
	end
	
	if nclcode == nil then
		return false
	end
	table.insert( ncl_trees, 1, xml:buildNclTree( nclcode ) )
	--print( #ncl_trees )
	--ncl_trees[#ncl_trees+1] = xml:buildNclTree( nclcode )
	local final_tree = xml:mergeNclTrees( ncl_trees )
	final_tree = xml:sortNclTree( final_tree )
	
	nclcode = xml:nclTreeToCode( final_tree, 0 ) 
	--nclcode = xml:indentCode( nclcode )
	nclcode = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' .. nclcode
	
	file:writeFile( 'target/main.ncl', nclcode )
	
	
	if file:exists( 'media/audio' ) then
		if not file:isEmpty( 'media/audio' ) then
			if file:exists('target/media') then		
				file:copy( 'media/audio', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/audio','target/media')
			end
		end
	end
	
	if file:exists( 'media/image' ) then
		if not file:isEmpty( 'media/image' ) then
			if file:exists('target/media') then		
				file:copy( 'media/image', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/image','target/media')
			end
		end
	end
	
	if file:exists( 'media/lua' ) then
		if not file:isEmpty( 'media/lua' ) then
			if file:exists('target/media') then		
				file:copy( 'media/lua', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/lua','target/media')
			end
		end
	end
	
	if file:exists( 'media/text' ) then
		if not file:isEmpty( 'media/text' ) then
			if file:exists('target/media') then		
				file:copy( 'media/text', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/text','target/media')
			end
		end
	end
	
	if file:exists( 'media/video' ) then
		if not file:isEmpty( 'media/video' ) then
			if file:exists('target/media') then		
				file:copy( 'media/video', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/video','target/media')
			end
		end
	end

	if file:exists( 'media/ncl' ) then
		if not file:isEmpty( 'media/ncl' ) then
			if file:exists('target/media') then		
				file:copy( 'media/ncl', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/ncl','target/media')
			end
		end
	end

	if file:exists( 'media/others' ) then
		if not file:isEmpty( 'media/others' ) then
			if file:exists('target/media') then		
				file:copy( 'media/others', 'target/media' )
			else
				file:createNewDir('target/media')
				file:copy('media/others','target/media')
			end
		end
	end
	
	return true
	
end

